#!/usr/bin/perl 
# fib2.pl
use warnings;
use strict;

sub fibonacci2 {
   my ($count, $internal) = @_;
                    
   if ($count <= 2) {
      # we know the answer already
      return $internal?[1,1]: 1;
   } else {
      # call ourselves to determine previous two elements
      my $result = fibonacci2($count -1, 'internal');
      # now we can calculate our element
      my $next = $result->[-1] + $result->[-2];
                    
      if ($internal) {
         push @{$result}, $next;
         return $result;
      } else {
         return $next;
      }     
   }
}               
                
foreach (1..20) { 
   print "Element $_ is ", fibonacci2($_), "\n";
}